package com.utonw.carloan.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.utonw.carloan.R;
import com.utonw.carloan.activity.main.MainActivity;
import com.utonw.carloan.bean.login.LoginBean;
import com.utonw.carloan.global.Constants;
import com.utonw.carloan.net.NetCallBack;
import com.utonw.carloan.net.NetTool;
import com.utonw.carloan.utils.LogUtil;
import com.utonw.carloan.utils.SharedPreferencesUtils;
import com.utonw.carloan.utils.UIUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录页
 */
public class LoginActivity extends AppCompatActivity {
    //登录按钮
    private TextView mLogin;
    //用户名
    private EditText mUserName;
    //密码
    private EditText mPassWord;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //取消标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //取消状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_login);

        //初始化组件
        mUserName = (EditText) findViewById(R.id.et_login_username);
        mPassWord = (EditText) findViewById(R.id.et_login_password);
        mLogin = (TextView) findViewById(R.id.tv_btn_login);


        //初始化EditText框
        initEditText(mUserName, mPassWord);

        //初始化数据
        initData();


    }

    private void initData() {
        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2017/4/1 登录逻辑
                Map<String, String> loginMap = new HashMap<>();
                loginMap.put("userName", mUserName.getText().toString());
                loginMap.put("passWord", mPassWord.getText().toString());

                NetTool.getInstance().startRequest(LoginActivity.this, true, Constants.LOGIN, loginMap, LoginBean.class, new NetCallBack<LoginBean>() {
                    @Override
                    public void onSuccess(LoginBean response) {

                        if (response.getRetCode().equals("000000")) {
                            SharedPreferencesUtils.saveToken(LoginActivity.this, response.getResult());
                            LogUtil.d(response.getResult());
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                            finish();
                        } else {
                            UIUtils.showShortToast(response.getMessage());
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                    }
                });

            }
        });
    }

    private void initEditText(final EditText userName, EditText password) {
        Drawable userIcon = UIUtils.getDrawable(R.mipmap.login_user_icon);
        Drawable pwdIcon = UIUtils.getDrawable(R.mipmap.login_password_icon);
        userIcon.setBounds(0, 0, 60, 60);
        pwdIcon.setBounds(0, 0, 60, 60);
        userName.setCompoundDrawables(userIcon, null, null, null);
        password.setCompoundDrawables(pwdIcon, null, null, null);

        ViewTreeObserver vto = userName.getViewTreeObserver();

        //布局绘制监听器，不依赖于xml文件
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                mLogin.setHeight(userName.getHeight());
                return true;
            }
        });


    }
}
